home *** CD-ROM | disk | FTP | other *** search
/ Amiga Format CD 7 / Amiga Format AFCD07 (Dec 1996, Issue 91).iso / serious / shareware / programming / aros / exec / availmem.c < prev    next >
Encoding:
C/C++ Source or Header  |  1996-09-13  |  3.4 KB  |  139 lines

  1. /*
  2.     (C) 1995-96 AROS - The Amiga Replacement OS
  3.     $Id: availmem.c,v 1.5 1996/09/13 17:51:22 digulla Exp $
  4.     $Log: availmem.c,v $
  5.     Revision 1.5  1996/09/13 17:51:22  digulla
  6.     Use IPTR
  7.  
  8.     Revision 1.4  1996/08/13 13:55:58  digulla
  9.     Replaced __AROS_LA by __AROS_LHA
  10.     Replaced some __AROS_LH*I by __AROS_LH*
  11.     Sorted and added includes
  12.  
  13.     Revision 1.3  1996/08/01 17:41:05  digulla
  14.     Added standard header for all files
  15.  
  16.     Desc:
  17.     Lang:
  18. */
  19. #include <exec/alerts.h>
  20. #include <exec/execbase.h>
  21. #include <aros/libcall.h>
  22.  
  23. /*****************************************************************************
  24.  
  25.     NAME */
  26.     #include <exec/memory.h>
  27.     #include <clib/exec_protos.h>
  28.  
  29.     __AROS_LH1(ULONG, AvailMem,
  30.  
  31. /*  SYNOPSIS */
  32.     __AROS_LHA(ULONG, attributes, D1),
  33.  
  34. /*  LOCATION */
  35.     struct ExecBase *, SysBase, 36, Exec)
  36.  
  37. /*  FUNCTION
  38.     Return either the total available memory or the largest available
  39.     chunk of a given type of memory.
  40.  
  41.     INPUTS
  42.     attributes - The same attributes you would give to AllocMem().
  43.  
  44.     RESULT
  45.     Either the total of the available memory or the largest chunk if
  46.     MEMF_LARGEST ist set in the attributes.
  47.  
  48.     NOTES
  49.     Due to the nature of multitasking the returned value may already
  50.     be obsolete if this function returns.
  51.  
  52.     EXAMPLE
  53.     Print the total available memory.
  54.  
  55.     printf("Free memory: %lu bytes\n",AvailMem(0));
  56.  
  57.     Print the size of the largest chunk of chip memory.
  58.  
  59.     printf("Largest chipmem chunk: %lu bytes\n",
  60.            AvailMem(MEMF_CHIP|MEMF_LARGEST));
  61.  
  62.     BUGS
  63.  
  64.     SEE ALSO
  65.  
  66.     INTERNALS
  67.  
  68.     HISTORY
  69.     15-10-95    created by m. fleischer
  70.  
  71. ******************************************************************************/
  72. {
  73.     __AROS_FUNC_INIT
  74.  
  75.     ULONG ret=0;
  76.     struct MemHeader *mh;
  77.  
  78.     /* Nobody else should access the memory lists now. */
  79.     Forbid();
  80.  
  81.     /* Get pointer to first memory header... */
  82.     mh=(struct MemHeader *)SysBase->MemList.lh_Head;
  83.     /* And follow the list. */
  84.     while(mh->mh_Node.ln_Succ!=NULL)
  85.     {
  86.         /*
  87.         The current memheader is OK if there's no bit in the
  88.         'attributes' that isn't set in the 'mh->mh_Attributes'.
  89.         MEMF_CLEAR, MEMF_REVERSE, MEMF_NO_EXPUNGE, MEMF_TOTAL and
  90.         MEMF_LARGEST are treated as if they were always set in
  91.         the memheader.
  92.         */
  93.         if(!(attributes&~(MEMF_CLEAR|MEMF_REVERSE|MEMF_NO_EXPUNGE
  94.             |MEMF_TOTAL|MEMF_LARGEST|mh->mh_Attributes)))
  95.         {
  96.         /* Find largest chunk? */
  97.         if(attributes&MEMF_LARGEST)
  98.         {
  99.             /*
  100.             Yes. Follow the list of MemChunks and set 'ret' to
  101.             each value that is bigger than all previous ones.
  102.             */
  103.             struct MemChunk *mc=mh->mh_First;
  104.             while(mc!=NULL)
  105.             {
  106. #if !defined(NO_CONSISTENCY_CHECKS)
  107.             /*
  108.                 Do some constistency checks:
  109.                 1. All MemChunks must be aligned to
  110.                    sizeof(struct MemChunk).
  111.                 2. The end (+1) of the current MemChunk
  112.                    must be lower than the start of the next one.
  113.             */
  114.             if(  ((IPTR)mc|mc->mc_Bytes)&(sizeof(struct MemChunk)-1)
  115.                ||(  (UBYTE *)mc+mc->mc_Bytes>=(UBYTE *)mc->mc_Next
  116.                   &&mc->mc_Next!=NULL))
  117.                 Alert(AT_DeadEnd|AN_MemoryInsane);
  118. #endif
  119.             if(mc->mc_Bytes>ret)
  120.                 ret=mc->mc_Bytes;
  121.             mc=mc->mc_Next;
  122.             }
  123.         }
  124.         else if(attributes&MEMF_TOTAL)
  125.             /* Determine total size. */
  126.             ret+=(STRPTR)mh->mh_Upper-(STRPTR)mh->mh_Lower;
  127.         else
  128.             /* Sum up free memory. */
  129.             ret+=mh->mh_Free;
  130.         }
  131.         mh=(struct MemHeader *)mh->mh_Node.ln_Succ;
  132.     }
  133.     /* All done. Permit dispatches and return. */
  134.     Permit();
  135.     return ret;
  136.     __AROS_FUNC_EXIT
  137. } /* AvailMem */
  138.  
  139.